using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

//角色生命值护甲相关

public class CharacterDataHolder : MonoBehaviour
{
    public event Action<int, int> UpdateHealthOnAttack;
    public event Action<int> UpdateDefenceOnAttack;
    public event Action<int, int> UpdateCourageOnAttack;
    public event Action<int> ShowDamageOnAttack;

    public CharacterData characterData = new CharacterData();

    public InjuredEffect injuredEffect;

    private void Start()
    {
        UpdateHealthOnAttack?.Invoke(CurrentHealth, MaxHealth);
    }

    public void SetCharacterData(CharacterData data)
    {
        characterData = data;
    }

    #region Attributes
    public int MaxHealth
    {
        get { return characterData.maxHealth; }
        set { characterData.maxHealth = value; }
    }
    public int CurrentHealth
    {
        get { return characterData.currentHealth; }
        set { characterData.currentHealth = value; }
    }

    public int CurrentDefence
    {
        get { return characterData.currentDefence; }
        set { characterData.currentDefence = value; }
    }
    public int CurrentCourage
    {
        get { return characterData.currentCourage; }
        set { characterData.currentCourage = value; }
    }
    public int MaxCourage 
    { 
        get { return characterData.maxCourage; }
        set { characterData.maxCourage = value; } 
    }
    public int CharacterID
    {
        get { return characterData.characterID; }
        set { characterData.characterID = value; }
    }



    #endregion

    public void TakeDamage(int damage, CharacterDataHolder defender)
    {
        if(!defender.characterData.isDead())
        {
            if (defender.CurrentDefence != 0)
            {
                int currentDamage = Mathf.Max(damage - defender.CurrentDefence, 0);
                TakeDefence(-damage, defender);
                if (currentDamage > 0)
                {
                    CurrentHealth = Mathf.Min(CurrentHealth - damage, MaxHealth);
                    CurrentHealth = Mathf.Max(CurrentHealth, 0);
                    UpdateHealthOnAttack?.Invoke(CurrentHealth, MaxHealth);//更新血条
                    ShowDamageOnAttack?.Invoke(damage);
                    injuredEffect.Injured();
                    if(CurrentHealth==0)
                    {
                        FightController.Instance.EnemyOnDie(this);
                    }
                }
            }
            else
            {
                CurrentHealth = Mathf.Min(CurrentHealth - damage, MaxHealth);
                CurrentHealth = Mathf.Max(CurrentHealth, 0);
                UpdateHealthOnAttack?.Invoke(CurrentHealth, MaxHealth);//更新血条
                ShowDamageOnAttack?.Invoke(damage);
                injuredEffect.Injured();
                if (CurrentHealth == 0)
                {
                    FightController.Instance.EnemyOnDie(this);
                }
            }
        }
    }

    public void TakeDefence(int num, CharacterDataHolder beUseder)
    {
        CurrentDefence = Mathf.Min(beUseder.CurrentDefence + num, 999);
        CurrentDefence = Mathf.Max(CurrentDefence, 0);
        UpdateDefenceOnAttack?.Invoke(CurrentDefence);
    }

    public void TakeCourage(int num,CharacterDataHolder beUseder)
    {
        CurrentCourage = Mathf.Min(beUseder.CurrentCourage + num, MaxCourage);
        CurrentCourage = Mathf.Max(CurrentCourage, 0);
        Debug.Log("courage=" + CurrentCourage + "/" + MaxCourage);
        UpdateCourageOnAttack?.Invoke(CurrentCourage, MaxCourage);
    }

    public void RemoveDefenceEveryTurn()
    {
        TakeDefence(-CurrentDefence, this);
    }

    public void ChangeMaxHP(int num)
    {
        MaxHealth += num;
        CurrentHealth += num;
        UpdateHealthOnAttack?.Invoke(CurrentHealth, MaxHealth);//更新血条
    }
}
